package utils;

public class 数字转金额大写工具类 {

    /**
     * 转金额大写工具类
     */
    // 数字数组：零，壹，贰，叁，肆，伍，陆，柒，捌，玖
    private static final String[] CHINESE_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    // 单位数组：个，十，百，千
    private static final String[] UNITS = {"", "拾", "佰", "仟"};
    // 分段单位数组：万亿，亿，万，元
    private static final String[] SECTIONS = {"", "万", "亿", "万亿", "亿亿"};

    public static String convertToChineseCurrency(double amount) {
        if (amount == 0) {
            return "零元整";
        }

        StringBuilder result = new StringBuilder();
        long integerPart = (long) amount;
        // 处理（角分）
        int decimalPart = (int) Math.round((amount - integerPart) * 100);

        // 处理整数部分
        result.append(convertSection(integerPart)).append("元");

        // 处理小数部分
        if (decimalPart > 0) {
            // 处理角
            int tenCent = decimalPart / 10;
            // 处理分
            int cent = decimalPart % 10;

            if (tenCent != 0) {
                result.append(CHINESE_NUMBERS[tenCent]).append("角");
            }
            if (cent != 0) {
                result.append(CHINESE_NUMBERS[cent]).append("分");
            }
        } else {
            result.append("整");
        }

        return result.toString();
    }

    /**
     * 处理角分前的单位
     *
     * @param number
     * @return
     */
    private static String convertSection(long number) {
        if (number == 0) {
            return CHINESE_NUMBERS[0];
        }

        StringBuilder section = new StringBuilder();
        boolean zero = true;
        int unitIndex = 0;

        while (number > 0) {

            long sectionNumber = number % 10000;
            if (sectionNumber != 0 || section.length() == 0) {
                section.insert(0, convertFourDigits(sectionNumber) + SECTIONS[unitIndex]);
                zero = false;
            } else if (!zero) {
                section.insert(0, CHINESE_NUMBERS[0]);
                zero = true;
            }
            number /= 10000;
            unitIndex++;
        }

        return section.toString();
    }

    private static String convertFourDigits(long number) {
        StringBuilder section = new StringBuilder();
        boolean zero = true;

        for (int i = 0; number > 0; i++, number /= 10) {
            int digit = (int) (number % 10);
            if (digit == 0) {
                if (!zero) {
                    section.insert(0, CHINESE_NUMBERS[0]);
                    zero = true;
                }
            } else {
                zero = false;
                // TODO 该部分代码还存在问题（超过13位之后会出现数据单位错乱问题）
                section.insert(0, CHINESE_NUMBERS[digit] + UNITS[i]);
            }
        }

        // 去除多余的零
        while (section.length() > 1 && section.charAt(0) == CHINESE_NUMBERS[0].charAt(0)) {
            section.deleteCharAt(0);
        }

        return section.toString();
    }

    public static void main(String[] args) {
        // 叁佰贰拾伍元整 - 格式错误
        double amount = 9223372036854775807.23;
        System.out.println("9223372036854775807.23:" + convertToChineseCurrency(amount));
        double amounVal = 1234567890987654321.23;
        System.out.println("1234567890987654321.23:" + convertToChineseCurrency(amounVal));

    }

}
